<?php
/*****************
 * Theme Options
 * 
 * 
 *****************/
add_action('admin_menu','create_theme_admin_menu');
function create_theme_admin_menu(){
	add_menu_page(__('Theme Options','larch'),__('R. Dan Website Theme Option','larch'),'administrator','theme_admin_page','build_theme_admin_menu_page');
}
function build_theme_admin_menu_page(){ ?>
<div class="wrap">
	<pre>
		<?php print_r(get_option('theme_options')); ?>
	</pre>
			<h1>Theme Options</h1>
			<?php settings_errors(); ?>
			<div class="container">
				<form method="post" action="options.php">
				<?php settings_fields('theme_options'); ?>
				<section class="row">
					<?php 
					 do_settings_sections('theme_options');
						submit_button();
					?>
				</section>
				</form>
			</div>
		</div>
<?php }
function get_theme_defaults(){
	$defaults = array(
		'show_login_button'=>1,
		'enable_social_media_icons'=>1,
		'enable_large_header_background'=>1,
		'footer_background_color'=>'#FEFEFE',
		'twitter_url' => 'https://www.twitter.com/',
		'facebook_url' => 'https://www.facebook.com/',
		'facebook_icon' => get_template_directory_uri().'/assets/img/facebook.png',
		'twitter_icon' => get_template_directory_uri().'/assets/img/twitter.png',
	);
	return $defaults;
}
add_action('admin_init','init_theme_options');
function init_theme_options(){
	//Add non-existing options
	if(false==get_option('theme_options')){add_option('theme_options',get_theme_defaults());}
	//delete_option('theme_options');
	//Register the newly created options
	register_setting('theme_options', 'theme_options');
	//Add sections to the form
	add_settings_section('theme_header_section', 'Header Settings', 'add_onto_header_section', 'theme_options');
	add_settings_section('theme_general_section', 'General Settings', 'add_onto_section', 'theme_options');
	add_settings_section('theme_footer_section', 'Footer Settings', 'add_onto_section', 'theme_options');
	//add_settings_section('theme_content_section', 'Content Settings', 'add_onto_section', 'theme_options');
	add_settings_section('theme_social_section', 'Social Media Settings', 'add_onto_section', 'theme_options');
	//add_settings_section('theme_seo_section', 'Search Engine Optimization Settings', 'add_onto_section', 'theme_options');
	/*Add input fields to the section and add those to the 'theme_options' collection
	 *
	 *Header Section Fields	 */
	add_settings_field('show_login_button', 'User Login Option', 'create_checkbox', 'theme_options', 'theme_header_section',
		array(
			'id'=>'show_login_button',
			'desc'=>'enable logging in',
		)
	);
	add_settings_field('enable_large_header_background', 'Large Header Background', 'create_checkbox', 'theme_options', 'theme_header_section',
		array(
			'id'=>'enable_large_header_background',
			'desc'=>'use large background image on for the header',
		)
	);
	add_settings_field('header_background_image', 'Header Background Image', 'create_upload_field', 'theme_options', 'theme_header_section',
		array(
			'id'=>'enable_header_background',
			'desc'=>'header large background image',
		)
	);
	//General Section Fields
	add_settings_field('use_custom_typos', 'Typography', 'create_checkbox', 'theme_options', 'theme_general_section',
		array(
			'id'=>'use_custom_typos',
			'desc'=>'choose own fonts on the various sections of a webpage',
		)
	);
	add_settings_field('content_typography', 'Content Font', 'create_checkbox', 'theme_options', 'theme_general_section',
		array(
			'id'=>'content_typography',
			'desc'=>'choose the font for your content',
		)
	);
	add_settings_field('header_typography', 'Header Font', 'create_checkbox', 'theme_options', 'theme_general_section',
		array(
			'id'=>'header_typography',
			'desc'=>'choose the font for your header',
		)
	);
	add_settings_field('footer_typography', 'Footer Font', 'create_checkbox', 'theme_options', 'theme_general_section',
		array(
			'id'=>'footer_typography',
			'desc'=>'choose the font for your footer',
		)
	);
	add_settings_field('color_scheme', 'Color Scheme', 'create_checkbox', 'theme_options', 'theme_general_section',
		array(
			'id'=>'color_scheme',
			'desc'=>'choose a preset color scheme',
		)
	);
	//Footer Section Fields
	add_settings_field('footer_background_color', 'Footer Background Color', 'create_text_field', 'theme_options', 'theme_footer_section',
		array(
			'id'=>'footer_background_color',
			'desc'=>'choose background of the footer',
		)
	);
	//Social Media Section Fields
	add_settings_field('enable_social_media_icons', 'Social Media Icons', 'create_checkbox', 'theme_options', 'theme_social_section',
		array(
			'id'=>'enable_social_media_icons',
			'desc'=>'show icons',
		)
	);
	add_settings_field('facebook_icon', 'Facebook Icon', 'create_upload_field', 'theme_options', 'theme_social_section',
		array(
			'id'=>'facebook_icon',
			'desc'=>'choose icon to use',
		)
	);
	add_settings_field('twitter_icon', 'Twitter Icon', 'create_upload_field', 'theme_options', 'theme_social_section',
		array(
			'id'=>'twitter_icon',
			'desc'=>'choose icon to use',
		)
	);
	add_settings_field('facebook_url', 'Facebook URL', 'create_text_field', 'theme_options', 'theme_social_section',
		array(
			'id'=>'facebook_url',
			'desc'=>'choose icon to use',
		)
	);
	add_settings_field('twitter_url', 'Twitter URL', 'create_text_field', 'theme_options', 'theme_social_section',
		array(
			'id'=>'twitter_url',
			'desc'=>'choose icon to use',
		)
	);
}
function add_onto_section(){
	//Copy paste this if a specific section needs additional markup
}
function add_onto_header_section(){ ?>
	<small>
		Other options can be found in Appearance Menu
	</small>
<?php }
function create_text_field($values){
	extract($values);
	$theme_option = get_option('theme_options');
	$theme_option[$id] = (isset($theme_option[$id])) ? esc_attr(stripcslashes($theme_option[$id])) : '' ;
	echo "<input type = 'text' id = '$id' name='theme_options[$id]' value ='$theme_option[$id]'/><label for='$id'>$desc</label>"; 
}
function create_upload_field($values){
	extract($values);
	$theme_option = get_option('theme_options');
	$theme_option[$id] = (isset($theme_option[$id])) ? esc_attr(stripcslashes($theme_option[$id])) : '' ;
	echo "<span class='upload'>
        <input type='text' class='regular-text text-upload' name='theme_options[$id]' value='$theme_option[$id]'/>
        <input type='button' class='button button-upload' value='Upload an image'/></br>
        <img style='max-width: 300px; display: block;' src='$theme_option[$id]' class='preview-upload' />
    </span>";
}
function create_checkbox($values){
	extract($values);
	$theme_option = get_option('theme_options');
	$theme_option[$id] = (isset($theme_option[$id])) ? esc_attr(stripcslashes($theme_option[$id])) : '' ;
	echo "<input id ='$id' name='theme_options[$id]' type='checkbox' value='1' ".checked(1,$theme_option[$id],false)."><label for='$id'>$desc</label>";
}
function create_textarea($values){
	extract($values);
	$theme_option = get_option('theme_options');
	$theme_option[$id] = (isset($theme_option[$id])) ? esc_attr(stripcslashes($theme_option[$id])) : '' ;
	echo "<textarea name='theme_options[$id]' id ='$id'>$theme_option[$id]</textarea>";
}
function create_select($values){
	extract($values);
}